<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Models\User;
use App\Http\Models\Goods;
use App\Http\Models\GoodsCoupon;
use App\Http\Models\GoodsShop;
use App\Http\Models\GoodsSales;
use App\Http\Models\GoodsCategory;
use App\Http\Models\TaobaoCategory;
use DB;
use Illuminate\Support\Facades\Cache;
use Encore\Admin\Auth\Permission;
use Encore\Admin\Facades\Admin;

/**
 * 用户商品控制
 * Creater: lqr
 */
class GoodsController extends Controller
{
    /**
     * 爆款页面查询顶部商品类目数据
     * @return string
     */
    public function category()
    {
        $list = Cache::remember('category', 10, function () {
            return GoodsCategory::select('id', 'name')->where('pid', '0')->orderBy('ordid', 'asc')->get();
        });
        return json_encode(['code' => 0, 'msg' => 'ok', 'data' => $list]);
    }

    /**
     * 首页查询爆款商品数据
     * @return string
     */
    public function fire()
    {
        $whs = array(
            'goods.goodstype' => 1,
            'goods.goodsstate' => 2
        );
        $intypes = null;//定义活动类型数组
        $key = 'fire_';
        //获取是否有聚划算或淘抢购
        $sid = $this->request->sid;
        if (!empty($sid)) {
            if ($sid == 1 or $sid == 2) {
                $whs['goods_shop.activitytype'] = $sid;//添加条件
            } elseif ($sid == 3) {
                $intypes = [1, 2];
            }
        }
        //排序类型
        $oid = $this->request->oid;
        //默认是综合
        $ob = array(
            'name' => 'goods.id',
            'val' => 'desc'
        );
        //最新
        if ($oid == 1) {
            $ob['name'] = 'goods.id';
        } elseif ($oid == 2) {//销量
            $ob['name'] = 'goods_sales.increment';
        } elseif ($oid == 3) {//价格
            $ob['name'] = 'goods.voucherprice';
            $ob['val'] = 'asc';
        } elseif ($oid == 4) {//佣金
            $ob['name'] = 'goods.commissionrate';
        } elseif ($oid == 5) {//面额
            $ob['name'] = 'goods_coupon.couponmoney';
        }
        $key = $key . $oid;
        //获取类目ID
        $cid = $this->request->cid;
        //如果有查询类目下面数据
        if (!empty($cid)) {
            $whs['goods.cateid'] = $cid;//添加条件
            $key = $key . $oid . '_' . $cid;
        }
        $page = request('page') ? request('page') : 1;
        $key = $key . '_' . $page;
        //每页显示数据个数
        $limit = env('LIMIT');
        //如果没有选择类目
        $list = getList($key, $whs, $limit, $ob, $intypes);

        return json_encode(['code' => 0, 'msg' => 'ok', 'data' => $list]);
    }

    /**
     * 秒杀平台商品
     * @return string
     */
    public function kill()
    {
        $whs = array(
            'goods.goodstype' => 2,
            'goods.goodsstate' => 1
        );

        //如果没有选择类目
        $list = DB::table('goods')
            ->leftjoin('goods_shop', 'goods.id', '=', 'goods_shop.gid')
            ->leftjoin('goods_sales', 'goods.id', '=', 'goods_sales.gid')
            ->leftjoin('goods_coupon', 'goods.id', '=', 'goods_coupon.gid')
            ->leftjoin('goods_kill', 'goods.id', '=', 'goods_kill.gid')
            ->leftjoin('users', 'goods.userid', '=', 'users.id')
            ->select('users.name as username',
                'goods.goodstitle', 'goods.coverimage', 'goods.commissiontype', 'goods.commissionrate', 'goods.copywriting', 'goods.copywritingimage', 'goods.origprice', 'goods.voucherprice', 'goods.created_at',
                'goods_coupon.couponlink', 'goods_coupon.coupontotal', 'goods_coupon.couponremain', 'goods_coupon.couponmoney', 'goods_coupon.reduceprice',
                'goods_sales.monthsales', 'goods_sales.increment', 'goods_sales.onehoursales', 'goods_sales.twohoursales', 'goods_sales.todaysales',
                'goods_kill.keyword', 'goods_kill.startdate', 'goods_kill.startfield', 'goods_kill.comment',
                'goods_shop.shoptype', 'goods_shop.shopname', 'goods_shop.activitytype')
            ->where($whs)->whereRaw('date_add(str_to_date(CONCAT(goods_kill.startdate," ",goods_kill.startfield,":00"),"%Y-%m-%d %H:%i:%s"), interval 1 day)>now()')->orderBy('startdate', 'asc')->orderBy('startfield', 'asc')->get();
        return json_encode(['code' => 0, 'msg' => 'ok', 'data' => $list]);
    }

    /**
     * 秒杀商品详情
     * @return string
     */
    public function killDesc()
    {
        $id = $this->request->id;//商品ID
        if (empty($id)) {
            return json_encode(['code' => 1, 'msg' => '缺少必须的商品ID！']);
        }
        $whs = array(
            'goods.id' => $id,
            'goods.goodstype' => 2,
            'goods.goodsstate' => 1
        );
        //如果没有选择类目
        $list = DB::table('goods')
            ->leftjoin('goods_shops', 'goods.id', '=', 'goods_shop.gid')
            ->leftjoin('goods_sales', 'goods.id', '=', 'goods_sales.gid')
            ->leftjoin('goods_coupon', 'goods.id', '=', 'goods_coupon.gid')
            ->leftjoin('goods_kill', 'goods.id', '=', 'goods_kill.gid')
            ->leftjoin('users', 'goods.userid', '=', 'users.id')
            ->select('users.name as username', 'users.avatar', 'users.score', 'users.created_at as jointime', 'users.qq',
                'goods.goodstitle', 'goods.coverimage', 'goods.commissiontype', 'goods.commissionrate', 'goods.copywriting', 'goods.copywritingimage', 'goods.origprice', 'goods.voucherprice', 'goods.created_at',
                'goods_coupon.couponlink', 'goods_coupon.coupontotal', 'goods_coupon.couponremain', 'goods_coupon.couponmoney', 'goods_coupon.reduceprice',
                'goods_sales.monthsales', 'goods_sales.increment', 'goods_sales.onehoursales', 'goods_sales.twohoursales', 'goods_sales.todaysales',
                'goods_kill.content', 'goods_kill.friends', 'goods_kill.viewcount',
                'goods_shop.shoptype', 'goods_shop.shopname', 'goods_shop.activitytype')
            ->where($whs)->get();
        if (empty($list)) {
            return json_encode(['code' => 1, 'msg' => '不存在的秒杀商品！']);
        }

        GoodsKill::where('gid', $id)->increment('viewcount')->save();

        return json_encode(['code' => 0, 'msg' => 'ok', 'data' => $list]);
    }

    /**
     * 前台实时上新商品数据
     * @return string
     */
    public function newgoods()
    {
        $whs = array(
            'goods.goodstype' => 0,
            'goods.goodsstate' => 1
        );
        $intypes = null;//定义活动类型数组
        $key = 'news_';
        //获取是否有聚划算或淘抢购
        $sid = $this->request->sid;
        if (!empty($sid)) {
            if ($sid == 1 or $sid == 2) {
                $whs['goods_shop.activitytype'] = $sid;//添加条件
            } elseif ($sid == 3) {
                $intypes = [1, 2];
            }
        }
        //排序类型
        $oid = $this->request->oid;
        //默认是综合
        $ob = array(
            'name' => 'goods.created_at',
            'val' => 'desc'
        );
        //最新
        if ($oid == 1) {
            $ob['name'] = 'goods.created_at';
        } elseif ($oid == 2) {//销量
            $ob['name'] = 'goods_sales.increment';
        } elseif ($oid == 3) {//价格
            $ob['name'] = 'goods.voucherprice';
            $ob['val'] = 'asc';
        } elseif ($oid == 4) {//佣金
            $ob['name'] = 'goods.commissionrate';
        } elseif ($oid == 5) {//面额
            $ob['name'] = 'goods_coupon.couponmoney';
        }
        $key = $key . $oid;
        //获取类目ID
        $cid = $this->request->cid;
        //如果有查询类目下面数据
        if (!empty($cid)) {
            $whs['goods.cateid'] = $cid;//添加条件
            $key = $key . $oid . '_' . $cid;
        }
        $page = request('page') ? request('page') : 1;
        $key = $key . '_' . $page;
        //每页显示数据个数
        $limit = env('LIMIT');
        //如果没有选择类目
        $list = getList($key, $whs, $limit, $ob, $intypes);
        return json_encode(['code' => 0, 'msg' => 'ok', 'data' => $list]);
    }

    /**
     * 前台销量排行商品数据
     * @return string
     */
    public function ranking()
    {
        $whs = array(
            'goods.goodstype' => 0,
            'goods.goodsstate' => 1
        );
        $intypes = null;//定义活动类型数组
        $key = 'ranking_';

        //排序类型
        $oid = $this->request->oid;
        //默认是一小时销量
        $ob = array(
            'name' => 'goods_sales.onehoursales',
            'val' => 'desc'
        );
        //两小时销量
        if ($oid == 2) {
            $ob['name'] = 'goods_sales.twohoursales';
        } elseif ($oid == 3) {//全天
            $ob['name'] = 'goods_sales.todaysales';
        } elseif ($oid == 4) {//佣金
            $ob['name'] = 'goods.commissionrate';
        }
        $page = request('page') ? request('page') : 1;
        $key = $key . $oid . '_' . $page;
        //每页显示数据个数
        $limit = env('LIMIT');
        //如果没有选择类目
        $list = getList($key, $whs, $limit, $ob, $intypes);
        return json_encode(['code' => 0, 'msg' => 'ok', 'data' => $list]);
    }

    /**
     * 前台预约商品数据
     * @return string
     */
    public function appointment()
    {
        $whs = array();
        $intypes = null;//定义活动类型数组
        //$key='appointment_';
        //获取是否有聚划算或淘抢购
        $sid = $this->request->sid;
        if (!empty($sid)) {
            if ($sid == 1 or $sid == 2) {
                $whs['goods_shop.activitytype'] = $sid;//添加条件
            } elseif ($sid == 3) {
                $intypes = [1, 2];
            }
        }
        //排序类型
        $oid = $this->request->oid;
        //默认是综合
        $ob = array(
            'name' => 'goods.created_at',
            'val' => 'desc'
        );
        //最新
        if ($oid == 1) {
            $ob['name'] = 'goods.created_at';
        } elseif ($oid == 2) {//销量
            $ob['name'] = 'goods_sales.increment';
        } elseif ($oid == 3) {//价格
            $ob['name'] = 'goods.voucherprice';
            $ob['val'] = 'asc';
        } elseif ($oid == 4) {//佣金
            $ob['name'] = 'goods.commissionrate';
        } elseif ($oid == 5) {//面额
            $ob['name'] = 'goods_coupon.couponmoney';
        }
        //$key=$key.$oid;
        //获取类目ID
        $cid = $this->request->cid;
        $limit = env('LIMIT');
        //如果有查询类目下面数据
        if (!empty($cid)) {
            $whs['goods.cateid'] = $cid;//添加条件
            //$key=$key.$oid.'_'.$cid;
        }
        //如果没有选择类目
        if (empty($intypes)) {
            $list = DB::table('goods')
                ->leftjoin('goods_shop', 'goods.id', '=', 'goods_shop.gid')
                ->leftjoin('goods_sales', 'goods.id', '=', 'goods_sales.gid')
                ->leftjoin('goods_coupon', 'goods.id', '=', 'goods_coupon.gid')
                ->leftjoin('users', 'goods.userid', '=', 'users.id')
                ->select('users.name as username',
                    'goods.goodstitle', 'goods.coverimage', 'goods.commissiontype', 'goods.commissionrate', 'goods.copywriting', 'goods.copywritingimage', 'goods.origprice', 'goods.voucherprice', 'goods.created_at', 'goods.begintime',
                    'goods_coupon.couponlink', 'goods_coupon.coupontotal', 'goods_coupon.couponremain', 'goods_coupon.couponmoney', 'goods_coupon.reduceprice',
                    'goods_sales.monthsales', 'goods_sales.increment', 'goods_sales.onehoursales', 'goods_sales.twohoursales', 'goods_sales.todaysales',
                    'goods_shop.shoptype', 'goods_shop.shopname', 'goods_shop.activitytype')
                ->where(function ($query) {
                    $query->where('goods.goodstype', '=', 1)->where('goods.goodsstate', '=', 2);
                })->orWhere(function ($query) {
                    $query->where('goods.goodstype', '=', 0)->where('goods.goodsstate', '=', 1);
                })->where($whs)->where('goods.begintime', '>', 'now()')->orderBy($ob['name'], $ob['val'])->paginate($limit);
            return json_encode(['code' => 0, 'msg' => 'ok', 'data' => $list]);
        } else {
            $list = DB::table('goods')
                ->leftjoin('goods_shop', 'goods.id', '=', 'goods_shop.gid')
                ->leftjoin('goods_sales', 'goods.id', '=', 'goods_sales.gid')
                ->leftjoin('goods_coupon', 'goods.id', '=', 'goods_coupon.gid')
                ->leftjoin('users', 'goods.userid', '=', 'users.id')
                ->select('users.name as username',
                    'goods.goodstitle', 'goods.coverimage', 'goods.commissiontype', 'goods.commissionrate', 'goods.copywriting', 'goods.copywritingimage', 'goods.origprice', 'goods.voucherprice', 'goods.created_at',
                    'goods_coupon.couponlink', 'goods_coupon.coupontotal', 'goods_coupon.couponremain', 'goods_coupon.couponmoney', 'goods_coupon.reduceprice',
                    'goods_sales.monthsales', 'goods_sales.increment', 'goods_sales.onehoursales', 'goods_sales.twohoursales', 'goods_sales.todaysales',
                    'goods_shop.shoptype', 'goods_shop.shopname', 'goods_shop.activitytype')
                ->where(function ($query) {
                    $query->where('goods.goodstype', '=', 1)->where('goods.goodsstate', '=', 2);
                })->orWhere(function ($query) {
                    $query->where('goods.goodstype', '=', 0)->where('goods.goodsstate', '=', 1);
                })->where($whs)->where('goods.begintime', '>', 'now()')->whereIn('goods_shop.activitytype', $intypes)->orderBy($ob['name'], $ob['val'])->paginate($limit);
            return json_encode(['code' => 0, 'msg' => 'ok', 'data' => $list]);
        }
    }

    /**
     * 前台搜索商品数据
     * @return string
     */
    public function search()
    {
        $key = $this->request->key;
        if (empty($key)) {
            return json_encode(['code' => 1, 'msg' => '缺少必要的搜索内容']);
        }
        $islike = false;
        $whs = array();
        //判断是否是搜索商品ID
        if (is_numeric($key)) {
            $whs['goods.goodsid'] = $key;
        } elseif (strpos($key, "http") === 0) {
            $goodid = get_id($key);
            if (empty($goodid)) {
                return json_encode(['code' => 1, 'msg' => '链接地址不正确']);
            } else {
                $whs['goods.goodsid'] = $goodid;
            }
        } else {
            $islike = true;//是Like查询
        }

        $intypes = null;//定义活动类型数组
        //获取是否有聚划算或淘抢购
        $sid = $this->request->sid;
        if (!empty($sid)) {
            if ($sid == 1 or $sid == 2) {
                $whs['goods_shop.activitytype'] = $sid;//添加条件
            } elseif ($sid == 3) {
                $intypes = [1, 2];
            }
        }
        //排序类型
        $oid = $this->request->oid;
        //默认是综合
        $ob = array(
            'name' => 'goods.created_at',
            'val' => 'desc'
        );
        //最新
        if ($oid == 1) {
            $ob['name'] = 'goods.created_at';
        } elseif ($oid == 2) {//销量
            $ob['name'] = 'goods_sales.increment';
        } elseif ($oid == 3) {//价格
            $ob['name'] = 'goods.voucherprice';
            $ob['val'] = 'asc';
        } elseif ($oid == 4) {//佣金
            $ob['name'] = 'goods.commissionrate';
        } elseif ($oid == 5) {//面额
            $ob['name'] = 'goods_coupon.couponmoney';
        }
        //获取类目ID
        $cid = $this->request->cid;
        $limit = env('LIMIT');
        //如果有查询类目下面数据
        if (!empty($cid)) {
            $whs['goods.cateid'] = $cid;//添加条件
        }
        $list = null;
        //如果没有选择类目
        if (empty($intypes)) {
            if ($islike) {
                $list = DB::table('goods')
                    ->leftjoin('goods_shop', 'goods.id', '=', 'goods_shop.gid')
                    ->leftjoin('goods_sales', 'goods.id', '=', 'goods_sales.gid')
                    ->leftjoin('goods_coupon', 'goods.id', '=', 'goods_coupon.gid')
                    ->leftjoin('users', 'goods.userid', '=', 'users.id')
                    ->select('users.name as username',
                        'goods.goodstitle', 'goods.coverimage', 'goods.commissiontype', 'goods.commissionrate', 'goods.copywriting', 'goods.copywritingimage', 'goods.origprice', 'goods.voucherprice', 'goods.created_at', 'goods.begintime',
                        'goods_coupon.couponlink', 'goods_coupon.coupontotal', 'goods_coupon.couponremain', 'goods_coupon.couponmoney', 'goods_coupon.reduceprice',
                        'goods_sales.monthsales', 'goods_sales.increment', 'goods_sales.onehoursales', 'goods_sales.twohoursales', 'goods_sales.todaysales',
                        'goods_shop.shoptype', 'goods_shop.shopname', 'goods_shop.activitytype')
                    ->where(function ($query) {
                        $query->where('goods.goodstype', '=', 1)->where('goods.goodsstate', '=', 2);
                    })->orWhere(function ($query) {
                        $query->where('goods.goodstype', '=', 0)->where('goods.goodsstate', '=', 1);
                    })->where($whs)->where('goods.goodstitle', 'like', '%' . $key . '%')->orderBy($ob['name'], $ob['val'])->paginate($limit);
            } else {
                $list = DB::table('goods')
                    ->leftjoin('goods_shop', 'goods.id', '=', 'goods_shop.gid')
                    ->leftjoin('goods_sales', 'goods.id', '=', 'goods_sales.gid')
                    ->leftjoin('goods_coupon', 'goods.id', '=', 'goods_coupon.gid')
                    ->leftjoin('users', 'goods.userid', '=', 'users.id')
                    ->select('users.name as username',
                        'goods.goodstitle', 'goods.coverimage', 'goods.commissiontype', 'goods.commissionrate', 'goods.copywriting', 'goods.copywritingimage', 'goods.origprice', 'goods.voucherprice', 'goods.created_at', 'goods.begintime',
                        'goods_coupon.couponlink', 'goods_coupon.coupontotal', 'goods_coupon.couponremain', 'goods_coupon.couponmoney', 'goods_coupon.reduceprice',
                        'goods_sales.monthsales', 'goods_sales.increment', 'goods_sales.onehoursales', 'goods_sales.twohoursales', 'goods_sales.todaysales',
                        'goods_shop.shoptype', 'goods_shop.shopname', 'goods_shop.activitytype')
                    ->where(function ($query) {
                        $query->where('goods.goodstype', '=', 1)->where('goods.goodsstate', '=', 2);
                    })->orWhere(function ($query) {
                        $query->where('goods.goodstype', '=', 0)->where('goods.goodsstate', '=', 1);
                    })->where($whs)->orderBy($ob['name'], $ob['val'])->paginate($limit);
            }


        } else {
            if ($islike) {
                $list = DB::table('goods')
                    ->leftjoin('goods_shop', 'goods.id', '=', 'goods_shop.gid')
                    ->leftjoin('goods_sales', 'goods.id', '=', 'goods_sales.gid')
                    ->leftjoin('goods_coupon', 'goods.id', '=', 'goods_coupon.gid')
                    ->leftjoin('users', 'goods.userid', '=', 'users.id')
                    ->select('users.name as username',
                        'goods.goodstitle', 'goods.coverimage', 'goods.commissiontype', 'goods.commissionrate', 'goods.copywriting', 'goods.copywritingimage', 'goods.origprice', 'goods.voucherprice', 'goods.created_at',
                        'goods_coupon.couponlink', 'goods_coupon.coupontotal', 'goods_coupon.couponremain', 'goods_coupon.couponmoney', 'goods_coupon.reduceprice',
                        'goods_sales.monthsales', 'goods_sales.increment', 'goods_sales.onehoursales', 'goods_sales.twohoursales', 'goods_sales.todaysales',
                        'goods_shop.shoptype', 'goods_shop.shopname', 'goods_shop.activitytype')
                    ->where(function ($query) {
                        $query->where('goods.goodstype', '=', 1)->where('goods.goodsstate', '=', 2);
                    })->orWhere(function ($query) {
                        $query->where('goods.goodstype', '=', 0)->where('goods.goodsstate', '=', 1);
                    })->where($whs)->where('goods.goodstitle', 'like', '%' . $key . '%')->whereIn('goods_shop.activitytype', $intypes)->orderBy($ob['name'], $ob['val'])->paginate($limit);
            } else {
                $list = DB::table('goods')
                    ->leftjoin('goods_shop', 'goods.id', '=', 'goods_shop.gid')
                    ->leftjoin('goods_sales', 'goods.id', '=', 'goods_sales.gid')
                    ->leftjoin('goods_coupon', 'goods.id', '=', 'goods_coupon.gid')
                    ->leftjoin('users', 'goods.userid', '=', 'users.id')
                    ->select('users.name as username',
                        'goods.goodstitle', 'goods.coverimage', 'goods.commissiontype', 'goods.commissionrate', 'goods.copywriting', 'goods.copywritingimage', 'goods.origprice', 'goods.voucherprice', 'goods.created_at',
                        'goods_coupon.couponlink', 'goods_coupon.coupontotal', 'goods_coupon.couponremain', 'goods_coupon.couponmoney', 'goods_coupon.reduceprice',
                        'goods_sales.monthsales', 'goods_sales.increment', 'goods_sales.onehoursales', 'goods_sales.twohoursales', 'goods_sales.todaysales',
                        'goods_shop.shoptype', 'goods_shop.shopname', 'goods_shop.activitytype')
                    ->where(function ($query) {
                        $query->where('goods.goodstype', '=', 1)->where('goods.goodsstate', '=', 2);
                    })->orWhere(function ($query) {
                        $query->where('goods.goodstype', '=', 0)->where('goods.goodsstate', '=', 1);
                    })->where($whs)->whereIn('goods_shop.activitytype', $intypes)->orderBy($ob['name'], $ob['val'])->paginate($limit);
            }
        }
        return json_encode(['code' => 0, 'msg' => 'ok', 'data' => $list]);
    }

    /**
     * 前台提交人用户主页数据
     * @return string
     */
    public function userindex()
    {
        $uid = $this->request->uid;
        if (empty($uid)) {
            return json_encode(['code' => 1, 'msg' => '缺少必要的用户ID']);
        }
        $user = DB::table('users as a')
            ->leftjoin('region as b', 'a.city_id', '=', 'b.id')
            ->leftjoin('region as c', 'a.province_id', '=', 'c.id')
            ->leftjoin('region as d', 'a.area_id', '=', 'd.id')
            ->select('a.name as username', 'a.phone', 'a.avatar', 'a.signature', 'a.score', 'a.qq',
                'b.name as city', 'c.name as province', 'd.name as area')->where('a.id', $uid)->get();
        if (empty($uid)) {
            return json_encode(['code' => 1, 'msg' => '不存在的用户信息']);
        }

        $whs = array(
            'goods.userid' => $uid
        );

        //排序类型
        $oid = $this->request->oid;
        //默认是综合
        $ob = array(
            'name' => 'goods.created_at',
            'val' => 'desc'
        );
        //最新
        if ($oid == 1) {
            $ob['name'] = 'goods.created_at';
        } elseif ($oid == 2) {//销量
            $ob['name'] = 'goods_sales.increment';
        } elseif ($oid == 3) {//价格
            $ob['name'] = 'goods.voucherprice';
            $ob['val'] = 'asc';
        } elseif ($oid == 4) {//佣金
            $ob['name'] = 'goods.commissionrate';
        }
        $limit = env('LIMIT');

        $list = DB::table('goods')
            ->leftjoin('goods_shop', 'goods.id', '=', 'goods_shop.gid')
            ->leftjoin('goods_sales', 'goods.id', '=', 'goods_sales.gid')
            ->leftjoin('goods_coupon', 'goods.id', '=', 'goods_coupon.gid')
            ->leftjoin('users', 'goods.userid', '=', 'users.id')
            ->select('users.name as username',
                'goods.goodstitle', 'goods.coverimage', 'goods.commissiontype', 'goods.commissionrate', 'goods.copywriting', 'goods.copywritingimage', 'goods.origprice', 'goods.voucherprice', 'goods.created_at', 'goods.begintime',
                'goods_coupon.couponlink', 'goods_coupon.coupontotal', 'goods_coupon.couponremain', 'goods_coupon.couponmoney', 'goods_coupon.reduceprice',
                'goods_sales.monthsales', 'goods_sales.increment', 'goods_sales.onehoursales', 'goods_sales.twohoursales', 'goods_sales.todaysales',
                'goods_shop.shoptype', 'goods_shop.shopname', 'goods_shop.activitytype')
            ->where(function ($query) {
                $query->where('goods.goodstype', '=', 1)->where('goods.goodsstate', '=', 2);
            })->orWhere(function ($query) {
                $query->where('goods.goodstype', '=', 0)->where('goods.goodsstate', '=', 1);
            })->where($whs)->orderBy($ob['name'], $ob['val'])->paginate($limit);

        $data = array(
            'userdata' => $user,
            'goodsdata' => $list
        );

        return json_encode(['code' => 0, 'msg' => 'ok', 'data' => $data]);
    }

    /**
     * 商品详情数据
     * @return string
     */
    public function goodsView()
    {
        $id = $this->request->id;
        if (empty($id)) {
            return json_encode(['code' => 1, 'msg' => '缺少必需的商品ID参数！']);
        }
        $datas = DB::table('goods')
            ->join('goods_shop', 'goods.id', '=', 'goods_shop.gid')
            ->join('goods_sales', 'goods.id', '=', 'goods_sales.gid')
            ->join('goods_coupon', 'goods.id', '=', 'goods_coupon.gid')
            ->select('goods.id', 'goods.goodsid', 'goods.goodslink', 'goods.goodstype',
                'goods.goodstitle', 'goods.coverimage', 'goods.commissiontype', 'goods.commissionrate', 'goods.copywriting', 'goods.copywritingimage', 'goods.voucherprice', 'goods.begintime',
                'goods_coupon.couponlink', 'goods_coupon.coupontotal', 'goods_coupon.coupontype',
                'goods_shop.activitytype')
            ->where('goods.id', $id)->whereIn('goods.goodsstate', [1, 2])->get();
        return json_encode(['code' => 0, 'msg' => 'ok', 'data' => $datas]);
    }




}
